// ProfileIni.cpp: implementation of the CProfileIni class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include "ProfileIni.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CProfileIni::CProfileIni()
{
}

CProfileIni::CProfileIni(CString pszFileFullPath, BOOL bFullPath)
{
    m_szFileName = pszFileFullPath;    
	if( !bFullPath )
	{
		GetIniFileFullPath(m_szFullPath);
	}
	else
		m_szFullPath = m_szFileName ; 
}

void CProfileIni::SetFileFullPath(CString filepath,BOOL bFullPath)
{
	m_szFileName = filepath;    
	if( !bFullPath )
	{
		GetIniFileFullPath(m_szFullPath);
	}
	else
		m_szFullPath = m_szFileName ; 	
}

CProfileIni::~CProfileIni()
{

}
int CProfileIni::GetInt(CString pszSectionName,CString pszKeyName)
{    
    int rc = GetPrivateProfileInt(pszSectionName, pszKeyName, 0, m_szFullPath);
    
    return rc;
}

int CProfileIni::SetInt(CString pszSectionName, CString pszKeyName, int nValue)
{    
	CString str ;
	str.Format(_T("%d"), nValue); 
    
    return WritePrivateProfileString(pszSectionName, pszKeyName, str, m_szFullPath);
}

int CProfileIni::SetFloat(CString pszSectionName, CString pszKeyName, float dValue)
{
	CString str ;
	str.Format(_T("%f"), dValue); 
    
    return WritePrivateProfileString(pszSectionName, pszKeyName, str, m_szFullPath);
}

float CProfileIni::GetFloat(CString pszSectionName, CString pszKeyName)
{
	TCHAR buf[512] ;
	CString szBuffer;
	bool msg=FALSE;

	int res = GetPrivateProfileString(pszSectionName, pszKeyName, NULL, buf, 512, m_szFullPath);
	szBuffer = buf ;
	if( msg && szBuffer.GetLength()==0 )
	{
		CString string ;
		string = pszSectionName ;
		string += _T( " : " );
		string += pszKeyName;
		AfxMessageBox(string+_T(" not have") );		
	}	
	float tt=0;
	tt=_wtof(szBuffer);
	return tt;
}


int CProfileIni::GetString(CString pszSectionName, CString pszKeyName, CString& szBuffer, BOOL msg)
{   
	TCHAR buf[512] ;

	int res = GetPrivateProfileString(pszSectionName, pszKeyName, NULL, buf, 512, m_szFullPath);
	szBuffer = buf ;
	if( msg && szBuffer.GetLength()==0 )
	{
		CString string ;
		string = pszSectionName ;
		string += _T( " : " );
		string += pszKeyName;
		AfxMessageBox(string+_T(" not have") );		
	}	
    return res ;
}

int CProfileIni::SetString(CString pszSectionName,CString pszKeyName,CString pszBuffer)
{   
    return WritePrivateProfileString(pszSectionName, pszKeyName, pszBuffer,m_szFullPath);
}

int CProfileIni::GetIniFileFullPath(CString& pszInifFileFullPath)
{
	TCHAR	path[_MAX_PATH];
	TCHAR	drv[_MAX_DRIVE];
	TCHAR	dir[_MAX_DIR];
	CString ext = _T("ini");

	int pos = m_szFileName.Find(_T("."));
	if( pos >= 0 )
	{
		ext = m_szFileName.Mid(pos+1); 
		m_szFileName = m_szFileName.Left(pos);
	}

	GetModuleFileName(NULL, path, _MAX_PATH);
	#ifdef _UNICODE
		_wsplitpath(path, drv, dir, NULL, NULL);	    
		_wmakepath(path, drv, dir, m_szFileName,ext);
	#else
		_splitpath(path, drv, dir, NULL, NULL);	    
		_makepath(path, drv, dir, m_szFileName,ext);
	#endif
	pszInifFileFullPath = path;
		
	return 0;
}


